using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_HeroEquipPetCell : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_HeroEquipPetCell
        {
            #region ObjectBinding Generate
            public UnityEngine.GameObject empty { protected set; get; }
            public UnityEngine.GameObject lockTip { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textLock { protected set; get; }
            public UnityEngine.GameObject details { protected set; get; }
            public IQIGame.Onigao.Framework.ExRawImage petIcon { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textName { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textNum { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("empty", out var __tbv0);
                this.empty = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("lockTip", out var __tbv1);
                this.lockTip = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textLock", out var __tbv2);
                this.textLock = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("details", out var __tbv3);
                this.details = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExRawImage>("petIcon", out var __tbv4);
                this.petIcon = __tbv4;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textName", out var __tbv5);
                this.textName = __tbv5;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textNum", out var __tbv6);
                this.textNum = __tbv6;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields
        private HeroData heroData = null;
        private Pet petData = null;
        private System.Action callOnClick;

        #endregion fields

        #region properties

        protected UIB_UIN_HeroEquipPetCell ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_HeroEquipPetCell();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            var exBtn = this.gameObject.GetOrAdd<ExButton>();
            exBtn.onClick.AddListener(this.OnClickSelf);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            this.ClearData();
        }

        #region methonds

        private void OnClickSelf()
        {
            this.callOnClick?.Invoke();
        }

        public void Show(HeroData heroData, System.Action funcClickPet)
        {
            this.SetData(heroData, funcClickPet);
            this.Show();
            this.ShowLock(heroData.level);

            this.ShowPet();
            this.ShowEmpty(this.petData == null);
        }

        private void SetData(HeroData heroData, System.Action funcClickPet)
        {
            this.heroData = heroData;
            this.callOnClick = funcClickPet;

            GameDataCenter.PlayerData.pet.TryGetValue(this.heroData.petId, out var pet);
            this.petData = pet;
        }

        private void ClearData()
        {
            this.heroData = null;
            this.callOnClick = null;
            this.petData = null;
        }

        private void ShowLock(int heroLv)
        {
            int limitLv = APIUIHeroInfo.LimitLvUnlockHeroPetColumn;
            bool isUnlock = limitLv <= heroLv;

            bool isActiveLock = !isUnlock;
            this.ui.lockTip.SetActive(isActiveLock);
            if (isActiveLock)
            {
                this.ui.textLock.text = APIUIHeroInfo.GetHeroPetColumnUnlockStr(limitLv);
            }
        }

        private void ShowPet()
        {
            bool isActive = this.petData != null;
            if (isActive)
            {
                var cfgPet = this.petData.cfgData;
                this.SetRawImageTexture(this.ui.petIcon, cfgPet.PetHalfBodyImage_fullPath);
                this.ui.textName.text = cfgPet.Name;
                this.ui.textNum.text = this.petData.level.ToString("00");
            }
            this.ui.details.SetActive(isActive);
        }

        private void ShowEmpty(bool isActive)
        {
            this.ui.empty.SetActive(isActive);
        }

        #endregion methonds
    }
}
